package com.boss.salary.service.impl;

import com.boss.common.util.DateUtils;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.IAgencyVerifyDao;
import com.boss.salary.dao.IFileInfoDao;
import com.boss.salary.dao.IPersonInfoDao;
import com.boss.salary.entity.FileInfo;
import com.boss.salary.service.IFileInfoService;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.config.CseCommonConfiguration;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FileInfoServiceImpl implements IFileInfoService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(FileInfoServiceImpl.class);

    @Autowired
    private IFileInfoDao iFileInfoDao;

    @Autowired
    private IPersonInfoDao iPersonInfoDao;

    @Autowired
    private IAgencyInfoDao iAgencyInfoDao;

    @Autowired
    private IAgencyVerifyDao iAgencyVerifyDao;

    public static final String SUCCESS_CODE = "100000";
    public static final String RSCODE = "rscode";
    /**
     * 引入模板配置类
     */
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 引入配置类
     */
    @Autowired
    private CseCommonConfiguration commonConfiguration;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertFileInfo(FileInfo fileInfo) {

            String salaryMonth = iAgencyVerifyDao.selectStateByAgencyId(fileInfo.getAgencyId(), "0","salary_month")
                    .get("salary_month").toString();
            fileInfo.setSalaryMonth(salaryMonth);
            fileInfo.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            fileInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            fileInfo.setMiddleTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            fileInfo.setIsDeleted(SystemDictConstant.IS_FALSE);
            List<String> children = fileInfo.getChildren();
        for (int i = 0; i < children.size(); i++) {
            fileInfo.setMiddleId(children.get(i));
            fileInfo.setId(SysUtils.generateGuid());
            iFileInfoDao.insertFileInfo(fileInfo);
        }

        return SUCCESS_CODE;
    }

    @Override
    public String insertFileInfoList(List<FileInfo> fileInfoList,UserDTO userDTO ) throws Exception {
        if(fileInfoList!=null&&fileInfoList.size()>0){
            Map<String, Object> requestMap=iAgencyVerifyDao.selectStateByAgencyId(fileInfoList.get(0).getAgencyId(), "0","salary_month");
            String salaryMonth = requestMap .get("salary_month").toString();
            for (FileInfo fileInfo:fileInfoList) {
                fileInfo.setMofDivCode(userDTO.getProvince());
                fileInfo.setSalaryMonth(salaryMonth);
                fileInfo.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                fileInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                fileInfo.setMiddleTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                fileInfo.setIsDeleted(SystemDictConstant.IS_FALSE);
                List<String> children = fileInfo.getChildren();
                if(children!=null&&children.size()>0){
                    for (int i = 0; i < children.size(); i++) {
                        fileInfo.setMiddleId(children.get(i));
                        fileInfo.setId(SysUtils.generateGuid());
                        iFileInfoDao.insertFileInfo(fileInfo);
                    }
                }else{
                    throw  new Exception();
                }
            }
        return SUCCESS_CODE;
        }else{
            throw  new Exception();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteFileInfo(FileInfo fileInfo) {
        //Integer count = iFileInfoDao.getFileInfoByFileId(fileInfo.getFileId());
        iFileInfoDao.deleteFileInfo(fileInfo.getId());
       /* if("1".equals(count.toString())){

            String url = //commonConfiguration.getFileservice()+"\n" +
                    "http://10.207.100.2:9008/v2/files";
            HashMap<String, Object> params = new HashMap<>();
            params.put("fileguids",fileInfo.getFileId());
            params.put("appid",SystemDictConstant.Appid);
            restTemplate.delete(url,params);
        }*/

        return SUCCESS_CODE;
    }

    @Override
    public String deleteEnclosureByIds(List<String> id) {
        iFileInfoDao.deleteEnclosureByIds(id);
        return SUCCESS_CODE;
    }

    @Override
    public List<FileInfo> getFileInfo(List<String> list, String operateType,String agencyId,String mofDivCode,UserDTO userDTO) {
        String salary_month = iAgencyVerifyDao.getSalaryMonth(userDTO.getProvince(),agencyId);
        List<FileInfo> fileInfo =new ArrayList<>();
        if(operateType!=null&&!"1".equals(operateType)){//人员附件
            fileInfo = iFileInfoDao.getFileInfo(list, operateType, agencyId,mofDivCode,salary_month);
        }
        if(operateType!=null&&"1".equals(operateType)){//单位附件
            fileInfo = iFileInfoDao.getFileInfoByAgencyId( agencyId,mofDivCode,salary_month);
        }
        return fileInfo;
    }

    @Override
    public Integer queryPersonFileNum(List<String> personId, String agencyId, String operateType,String mofDivCode) {
        int count = 0;
        for (int i = 0; i < personId.size(); i++) {
            String id =  personId.get(i);
            count = iFileInfoDao.queryPersonFileNum(id, agencyId, operateType,mofDivCode);
            if(count == 0){
                break;
            }
        }
        return count;
    }

    @Override
    public List<FileInfo> queryExcel(List<String> list, String mofDivCode,String agencyId,UserDTO userDTO) {
        String salary_month = iAgencyVerifyDao.getSalaryMonth(userDTO.getProvince(),agencyId);
        List<FileInfo> fileInfo = iFileInfoDao.queryExcel(list, agencyId,mofDivCode,salary_month);


        return fileInfo;
    }

}
